import requests,json
def next_item(item,list0):
    for i,key in enumerate(list0):
        if item==key:
            return i+1
            
code='001593'
num_days=300
url = 'http://api.fund.eastmoney.com/f10/lsjz?callback=jQuery18301935777817587765_1607703371648&fundCode={}&pageIndex=1&pageSize={}&startDate=&endDate=&_=1607703371710'.format(code,num_days)
header = {
     'Referer':'http://fundf10.eastmoney.com/jjjz_{}.html'.format(code)
}
req = requests.get(url,headers = header)

#print(req,dir(req))
x=req.text[41:-1]
#print(x)
state=json.loads(x,strict=False)
history=state['Data']['LSJZList']
history.sort(key=lambda x:x['FSRQ'])

first_day_price=history[0]['DWJZ']
first_date=history[0]['FSRQ']
last_day_price=history[-1]['DWJZ']
last_date=history[-1]['FSRQ']
print(code,'第一天',first_date,first_day_price,'最后一天',last_date,last_day_price,'营收率','{:.2%}'.format(float(last_day_price)/float(first_day_price)))
sum0=0
i=0
for item in history:
    #print(item['DWJZ'])
    sum0=sum0+float(item['DWJZ'])
    i=i+1
ava=sum0/i
    #print(item['DWJZ'],sum0/i)
print('每日定投均价','{:.5}'.format(ava),'定投收益率','{:.2%}'.format(float(last_day_price)/ava))
sum1=0
i1=0
print(type(history))
for item in history:
    #print(item['DWJZ'])
    if float(item['JZZZL'])<-1 and float(item['JZZZL'])>-1.2:
        print('下跌买入','当日',item['FSRQ'],item['JZZZL'],'下一日',history[next_item(item,history)]['FSRQ'],history[next_item(item,history)]['FSRQ'],history[next_item(item,history)]['JZZZL'])